package io.gitee.sfch.chunmi.conf.listener;

import com.mybatisflex.annotation.AbstractInsertListener;
import io.gitee.sfch.chunmi.entity.MutableEntity;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * {@link MutableEntity} 新增时增加新增、更新审计信息
 */
@Component
public class MutableEntityInsertListener extends AbstractInsertListener<MutableEntity> {

    @Resource
    private ImmutableEntityInsertListener insertListener;

    @Override
    public void doInsert(MutableEntity entity) {
        insertListener.doInsert(entity);
        this.setUpdateInfo(entity);
    }

    /**
     * 设置更新信息
     *
     * @param entity 实体
     */
    public void setUpdateInfo(MutableEntity entity) {
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdaterId(-1L);
        entity.setUpdaterName(entity.getUpdaterName());
    }
}
